*****************************************************************************************************************************************************************************************
****************************************************************************Turnout analyses*********************************************************************************************
*****************************************************************************************************************************************************************************************
cd "E:\workdata\704429\Projektfiles\Co-residence"
est clear
use analysis_out_data_all_pairs, clear

**Generate stacked datasæt**
gen id=_n
replace koen1 = koen1 - 1 
replace koen2 = koen2 - 1 

save temp_analysis1.dta, replace
foreach var in stemt residentialstability stemte_2009 pnr FOED_DAG koen  {
	rename `var'1 `var'3
	rename `var'2 `var'1
	rename `var'3 `var'2
}

append using temp_analysis1.dta

lab def koen 0 "male" 1 "female"
lab val koen1 koen
lab val koen2 koen

*Age*
gen alder_ved_valg1=2013-year(FOED_DAG1)
replace alder_ved_valg1=alder_ved_valg1-1 if month(FOED_DAG1)==12
replace alder_ved_valg1=alder_ved_valg1-1 if month(FOED_DAG1)==11 & day(FOED_DAG1)>19

gen alder_ved_valg2=2013-year(FOED_DAG2)
replace alder_ved_valg2=alder_ved_valg2-1 if month(FOED_DAG2)==12
replace alder_ved_valg2=alder_ved_valg2-1 if month(FOED_DAG2)==11 & day(FOED_DAG2)>19

gen HH_age=(alder_ved_valg1+alder_ved_valg2)/2

*****************************************************************************************************************************************************************************************
****************************************************************************Merge with other datasets************************************************************************************
*Get ethnicity*
foreach num of numlist 1/2 {
rename pnr`num' pnr
merge 1:1 pnr using "E:\workdata\704429\Grunddatanewest\bef2014.dta", keepusing(IE_TYPE civst)
drop if _m==2
drop _m
rename IE_TYPE IE_TYPE`num'
rename civst civst`num'
rename pnr pnr`num'
}

*Get education*
foreach num of numlist 1/2 {
rename pnr`num' pnr
merge 1:1 pnr using "E:\workdata\704429\Grunddatanewest\udda2014.dta", keepusing(hfaudd_hovedgruppe)
drop if _m==2
drop _m
rename hfaudd_hovedgruppe hfaudd_hovedgruppe`num'
rename pnr pnr`num'
}

*Get income*
foreach num of numlist 1/2 {
rename pnr`num' pnr
merge 1:1 pnr using "E:\workdata\704429\Grunddatanewest\indh2013.dta", keepusing(perindkialt)
drop if _m==2
drop _m
rename perindkialt perindkialt`num'
rename pnr pnr`num'
}


*Minor recodes*
replace stemte_20091 = 2 if stemte_20091 == .
replace stemte_20092 = 2 if stemte_20092 == .

*****************************************************************************************************************************************************************************************
gen month_factor=month13+83.5  
est clear

*Models for column 1 and 2 of Table 2
eststo: reg stemt1 b84.month_factor  ///
if month13_factor >= 72 & month13_factor <= 96 , cluster(id) 

eststo: reg stemt1 b84.month_factor  ///
IE_TYPE* hfaudd_h* koen* civst* i.stemte_2009* ///
c.alder_ved_valg1##c.alder_ved_valg1##c.alder_ved_valg1 ///
c.valg13##i.treatment, cluster(id) 

eststo: reg stemt1 b85.month_factor  ///
if month13_factor >= 72 & month13_factor <= 96 , cluster(id) 

eststo: reg stemt1 b85.month_factor  ///
IE_TYPE* hfaudd_h* koen* civst* i.stemte_2009* ///
c.alder_ved_valg1##c.alder_ved_valg1##c.alder_ved_valg1 ///
c.valg13##i.treatment , cluster(id) 

log using move_in_graphs/extra.log, append
tab month_factor if month13_factor >= 82 & month13_factor <= 85, sort // min. N Table 3A
tab month_factor if month13_factor >= 82 & month13_factor <= 85 & ///
IE_TYPE1 != . & hfaudd_hovedgruppe1 != . & koen1 != . & ///
 civst1 != . & stemte_20091 != . & ///
 IE_TYPE2 != . & hfaudd_hovedgruppe2 != . & koen2 != . & ///
 civst2 != . & stemte_20092 != ., sort // N is the same in mod2 and mod3
log close

********************************************************************
********************************************************************
********************Column 3&4**************************************
********************************************************************
********************************************************************

use analysis_out_data_all_pairs, clear

**Generate stacked datasæt**
gen id=_n
replace koen1 = koen1 - 1 
replace koen2 = koen2 - 1 

save temp_analysis1.dta, replace
foreach var in stemt residentialstability stemte_2009 pnr FOED_DAG koen  {
	rename `var'1 `var'3
	rename `var'2 `var'1
	rename `var'3 `var'2
}

append using temp_analysis1.dta

lab def koen 0 "male" 1 "female"
lab val koen1 koen
lab val koen2 koen

keep HH_id pnr1 stemte_20091 stemt1 dato1 FOED_DAG1 koen1 residentialstability1

rename (pnr1 stemte_20091 stemt1 dato1 FOED_DAG1 koen1 residentialstability1) ///
(pnr stemte_2009 stemt dato FOED_DAG koen residentialstability)

replace HH_id = HH_id + 10000000

append using disc_analysis_singles.dta

lab val koen koen

duplicates t HH_id, gen(couple) // generate variable for couples

**Generate dates**
gen valg13=dato-19681 // Cont. date 2013
gen valg09=dato-18218 // Cont. date 2009
gen month13=floor((valg13-1)/30)+0.5 // 1 month 2013
gen month09=floor((valg09/30-1))+0.5 // 1 month 2009
gen month13_factor=month13+83.5


* Add variables for later and save
gen treatment13 = valg13 <= 0 // 1=Had moved together on Election Day 0=Had not moved together on Election Day
gen interaction13=treatment13*month13 // Allow for different trends on each side
gen weight13 = 1 - (abs(month13)-0.5)/12 

*****************************************************************************************************************************************************************************************


*Get ethnicity*
merge 1:1 pnr using "E:\workdata\704429\Grunddatanewest\bef2014.dta", keepusing(IE_TYPE civst)
drop if _m==2
drop _m

*Get education*
merge 1:1 pnr using "E:\workdata\704429\Grunddatanewest\udda2014.dta", keepusing(hfaudd_hovedgruppe)
drop if _m==2
drop _m

*Get income*
merge 1:1 pnr using "E:\workdata\704429\Grunddatanewest\indh2013.dta", keepusing(perindkialt)
drop if _m==2
drop _m


*Minor recodes*
replace stemte_2009 = 2 if stemte_2009 == .


*****************************************************************************************************************************************************************************************
****************************************************************************TABLE 2******************************************************************************************************
*****************************************************************************************************************************************************************************************


gen month_factor=month13+83.5  
gen stemt_dif = stemt - stemte_2009 if stemte_2009 != 2

**Models for column 3 and 4

eststo: reg stemt b84.month_factor##i.couple ///
if month13_factor == 84 | month13_factor == 83, cluster(HH_id) 
eststo: reg stemt b85.month_factor##i.couple ///
if month13_factor == 85 | month13_factor == 82, cluster(HH_id) 

eststo: reg stemt b84.month_factor##i.couple ///
i.IE_TYPE i.hfaudd_h koen i.civst i.stemte_2009 ///
c.FOED_DAG##c.FOED_DAG##c.FOED_DAG ///
c.valg13##i.treatment , cluster(HH_id) 

eststo: reg stemt b85.month_factor##i.couple ///
i.IE_TYPE i.hfaudd_h koen i.civst i.stemte_2009 ///
c.FOED_DAG##c.FOED_DAG##c.FOED_DAG ///
c.valg13##i.treatment, cluster(HH_id) 


log using move_in_graphs/extra_DiD.log, append
tab month_factor couple if month13_factor >= 82 & month13_factor <= 85 & couple != . // min. N Table 3A
tab month_factor couple if month13_factor >= 82 & month13_factor <= 85 & couple != . ///
& IE_TYPE != . & hfaudd_h  != . & koen  != . & civst  != . & stemte_2009  != . ///
& FOED_DAG != . & treatment  != . // min. N Table 3A
log close


esttab est* , nostar replace ci b(3)
